
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )

include('shared.lua')

util.PrecacheModel( "models/ascension/objects/crate_01.mdl" )
ENT.AutomaticFrameAdvance = true

function ENT:Initialize()

	self:SetModel( "models/ascension/objects/crate_01.mdl" )

	self:PhysicsInit( SOLID_VPHYSICS )
	self:SetMoveType( MOVETYPE_VPHYSICS )
	self:SetSolid( SOLID_VPHYSICS )
	
	local phys = self:GetPhysicsObject()
	if ( phys:IsValid() ) then
		phys:Wake()
		phys:EnableMotion( false )
	end

	self:SetCollisionGroup( COLLISION_GROUP_WORLD )
	
	self:SetAngles( Angle(0,90,0) )
	self:StartMotionController()
	
	self.ShadowParams = {}
	self:SetNWBool( "active", false )
	self:SetNWInt( "numPlyTouch", 0 )
	self.SeqThink = 0
	
end

function ENT:ActivateBlock()

	//self:SetCollisionGroup( COLLISION_GROUP_WEAPON )
	self:SetNWBool( "active", true )
	self:SetSequence( self:LookupSequence("idle") )

end

function ENT:IsActive()

	return self:GetNWString( "active" )

end

function ENT:Think()

	if ( !self:GetNWBool( "active" ) ) then

		self:SetSequence( self:LookupSequence("hover") )
		self:SetPlaybackRate( 1 )
		
		if ( self.SeqThink < CurTime() ) then
			self.SeqThink = CurTime()+self:SequenceDuration()
			self:ResetSequence( self:LookupSequence("hover") )
		end
		
		self:NextThink( CurTime() )
		
		return true
	
	else

		self:NextThink( CurTime() + 100 )
		
		return true
	
	end

end

function ENT:SetTeam( teamid )

	self:SetNWInt( "team", teamid )
	
	if (teamid == TEAM_ORANGE) then
		self:SetSkin(3)
	elseif (teamid == TEAM_BLUE) then
		self:SetSkin(4)
	elseif (teamid == TEAM_RED) then
		self:SetSkin(1)
	elseif (teamid == TEAM_GREEN) then
		self:SetSkin(2)
	end
	
end

function ENT:GetTeam()
	return self:GetNWInt( "team" )
end

function ENT:PhysicsSimulate( phys, delta )
	
	local ply = self:GetOwner()
	
	if ( self:GetOwner() == NULL or not self:GetOwner():IsValid() ) then return SIM_NOTHING end
	
	phys:Wake()
	
	self.ShadowParams.secondstoarrive = 1 
	self.ShadowParams.pos = ply:GetPos() + Vector(0,0,54) + ply:GetAimVector() * 64 
	self.ShadowParams.angle = Angle( 0, 90, 0 )
	self.ShadowParams.maxangular = 5000 
	self.ShadowParams.maxangulardamp = 10000
	self.ShadowParams.maxspeed = 1000000
	self.ShadowParams.maxspeeddamp = 100
	self.ShadowParams.dampfactor = 0.1 
	self.ShadowParams.teleportdistance = 200
	self.ShadowParams.deltatime = delta
 
	phys:ComputeShadowControl(self.ShadowParams)
	
end
